Completed
Pull Request — future (#203)
by
unknown
36s
created

main.js ➔ ... ➔ config.dataPath.forEach   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 2
1
define(['moment', 'utils/router', 'leaflet', 'gui', 'helper', 'utils/language'],
2
  function (moment, Router, L, GUI, helper, Language) {
3
    'use strict';
4
5
    return function (config) {
6
      function handleData(data) {
7
        var timestamp;
8
        var nodes = [];
9
        var links = [];
10
        var nodeDict = {};
11
12
        for (var i = 0; i < data.length; ++i) {
13
          nodes = nodes.concat(data[i].nodes);
14
          timestamp = data[i].timestamp;
15
          links = links.concat(data[i].links);
16
        }
17
18
        nodes.forEach(function (node) {
19
          node.firstseen = moment.utc(node.firstseen).local();
20
          node.lastseen = moment.utc(node.lastseen).local();
21
        });
22
23
        var now = moment();
24
        var age = moment(now).subtract(config.maxAge, 'days');
25
26
        var online = nodes.filter(function (d) {
27
          return d.is_online;
28
        });
29
        var offline = nodes.filter(function (d) {
30
          return !d.is_online;
31
        });
32
33
        var newnodes = helper.limit('firstseen', age, helper.sortByKey('firstseen', online));
34
        var lostnodes = helper.limit('lastseen', age, helper.sortByKey('lastseen', offline));
35
36
        nodes.forEach(function (d) {
37
          d.neighbours = [];
38
          nodeDict[d.node_id] = d;
39
        });
40
41
        links.forEach(function (d) {
42
          d.source = nodeDict[d.source];
43
          d.target = nodeDict[d.target];
44
45
          d.id = [d.source.node_id, d.target.node_id].join('-');
46
          d.source.neighbours.push({ node: d.target, link: d });
47
          d.target.neighbours.push({ node: d.source, link: d });
48
49
          try {
50
            d.latlngs = [];
51
            d.latlngs.push(L.latLng(d.source.location.latitude, d.source.location.longitude));
52
            d.latlngs.push(L.latLng(d.target.location.latitude, d.target.location.longitude));
53
54
            d.distance = d.latlngs[0].distanceTo(d.latlngs[1]);
55
          } catch (e) {
56
            // ignore exception
57
          }
58
        });
59
60
        return {
61
          now: now,
62
          timestamp: moment.utc(timestamp).local(),
1 ignored issue
show
Bug introduced by
The variable timestamp seems to not be initialized for all possible execution paths. Are you sure utc handles undefined variables?
Loading history...
63
          nodes: {
64
            all: nodes,
65
            online: online,
66
            offline: offline,
67
            new: newnodes,
68
            lost: lostnodes
69
          },
70
          links: links,
71
          nodeDict: nodeDict
72
        };
73
      }
74
75
      var language = new Language(config);
76
      var router = new Router(language);
77
78
      language.init(router);
79
80
      function update() {
81
        return Promise.all(config.dataPath.map(helper.getJSON))
82
          .then(handleData);
83
      }
84
85
      update()
86
        .then(function (d) {
87
          var gui = new GUI(config, router, language);
88
          gui.setData(d);
89
          router.setData(d);
90
          router.resolve();
91
92
          window.setInterval(function () {
93
            update().then(function (n) {
94
              gui.setData(n);
95
              router.setData(n);
96
            });
97
          }, 60000);
98
        })
99
        .catch(function (e) {
100
          document.querySelector('.loader').innerHTML += e.message
101
            + '<br /><br /><button onclick="location.reload(true)" class="btn text" aria-label="Try to reload">Try to reload</button><br /> or report to your community';
102
          console.warn(e);
103
        });
104
    };
105
  });
106